//
//  HMHomeHeader.m
//  HowToDo
//
//  Created by 胡坤 on 2017/1/3.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMHomeHeader.h"
//#import "HMTopImgButton.h"
#import "HMTopILView.h"
#import <SDCycleScrollView.h>
#import <Masonry.h>
@interface HMHomeHeader ()<SDCycleScrollViewDelegate>
//轮播图
@property(nonatomic,strong)SDCycleScrollView* cycleView;
//存放选项按钮的视图
@property(nonatomic,strong)UIView* optionBtnsview;
@end
@implementation HMHomeHeader

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setUPUI];
    }
    return self;
}
-(void)setUPUI
{
    self.backgroundColor = [UIColor whiteColor];
    //无限轮播视图
    [self addSubview:self.cycleView];
    //频道选项视图
    [self addSubview:self.optionBtnsview];
    //添加约束
    [self.cycleView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.left.right.equalTo(self);
         make.height.mas_equalTo(120);
     }];
    [self.optionBtnsview mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.left.right.bottom.equalTo(self);
         make.top.equalTo(self.cycleView.mas_bottom);
     }];
}
#pragma mark -- delegate
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    self.clickCycleImg(index);
}
-(void)setCarouselList:(NSArray *)carouselList
{
    _carouselList = carouselList;
    self.cycleView.imageURLStringsGroup = carouselList;
}
//添加分类按钮
-(void)setClassfiList:(NSArray *)classfiList
{
    _classfiList = classfiList;
    if (self.optionBtnsview.subviews.count != 0)
    {
        [self.optionBtnsview.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj removeFromSuperview];
        }];
    }
    //遍历数据获取对应字段
    for (NSDictionary* dic in self.classfiList)
    {
        NSString* iconName = dic[@"HomeClassificationIcon"];
        NSString* classificationName = dic[@"HomeClassificationName"];
        NSString* pushVcName = dic[@"PushVcName"];
        HMTopILView* btn = [[HMTopILView alloc]init];
        btn.titleLable.font = [UIFont systemFontOfSize:12];
        btn.imgView.image = [UIImage imageNamed:iconName];
        btn.titleLable.text = classificationName;
        btn.titleLable.textColor = [UIColor lightGrayColor];
        btn.pushVcName = pushVcName;
        btn.programTp_Id = dic[@"type_id"];
        [btn addTarget:self action:@selector(optionBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [btn sizeToFit];
        [self.optionBtnsview addSubview:btn];
    }
    //判断防止崩溃
    if (self.optionBtnsview.subviews.count > 1)
    {
        [self.optionBtnsview.subviews mas_distributeViewsAlongAxis: MASAxisTypeHorizontal withFixedSpacing:20 leadSpacing:15 tailSpacing:15];
        [self.optionBtnsview.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
        {
           [obj mas_makeConstraints:^(MASConstraintMaker *make) {
               make.top.equalTo(self.optionBtnsview);
               make.height.equalTo(self.optionBtnsview);
           }];
        }];
    }
}
//分类按钮的点击事件
-(void)optionBtnAction:(HMTopILView*)sender
{
    self.clickBtnCallBack(sender);
}

-(SDCycleScrollView *)cycleView
{
    if (!_cycleView)
    {
        _cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImage imageNamed:@""]];
        //设置当前分页视图样式
        _cycleView.currentPageDotColor = [UIColor lightGrayColor];
        [_cycleView setCurrentPageDotImage:[UIImage imageNamed:@"header_cycle_current"]];
        //设置其他
        _cycleView.pageDotColor = [UIColor whiteColor];
        //自定义pagecontroll样式时设置大小
        [_cycleView setPageDotImage:[UIImage imageNamed:@"header_cycle_nor"]];
        _cycleView.pageControlDotSize = CGSizeMake(15, 2.5);
        _cycleView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    }
    return _cycleView;
}
- (UIView *)optionBtnsview
{
    if (!_optionBtnsview)
    {
        _optionBtnsview = [[UIView alloc]init];
    }
    return _optionBtnsview;
}

@end
